import axios from 'axios'
import type { AxiosInstance } from 'axios'
import { YcRequestInterceptors, YcRequestConfig } from './type'
// axios -> axios实例
const DEFAULT_LOADING = true

class YcRequest {
  instance: AxiosInstance
  interceptors?: YcRequestInterceptors
  Loading: any
  showLoading: boolean
  constructor(config: YcRequestConfig) {
    this.instance = axios.create(config)
    this.interceptors = config.interceptors
    this.showLoading = config.showLoading ?? DEFAULT_LOADING //默认显示loading
    //todo 请求拦截(对于Request实例的拦截器)
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    )
    // 响应拦截
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )

    //todo 添加所有实例都有的拦截器(公共拦截器)
    this.instance.interceptors.request.use(
      (res) => {
        // console.log('所有的实例都有的拦截器：请求成功拦截')

        if (this.showLoading) {
          this.Loading = ElLoading.service({
            lock: true, //蒙版
            text: 'Loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
        }
        return res
      },
      (err) => {
        this.showLoading && this.Loading.close()
        // console.log('所有的实例都有的拦截器：请求失败拦截')
        console.log('请求发送失败', err)
      }
    )
    this.instance.interceptors.response.use(
      (res) => {
        // 先执行
        this.showLoading && this.Loading.close()

        return res.data
      },
      (err) => {
        this.showLoading && this.Loading.close()
        return err
      }
    )
  }

  request<T>(config: YcRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.interceptors?.requestInterceptor) {
        config = config.interceptors?.requestInterceptor(config)
      }

      if (config.showLoading === false) {
        // 单个请求取消loading
        this.showLoading = config.showLoading
      }

      this.instance
        .request<any, T>(config)
        .then((res) => {
          //1. 单个请求对数据的处理
          if (config.interceptors?.responseInterceptor) {
            res = config.interceptors?.responseInterceptor(res)
            console.log('单个请求的拦截')
          }

          this.showLoading = DEFAULT_LOADING
          resolve(res)
        })
        .catch((err) => {
          this.showLoading = DEFAULT_LOADING

          reject(err)
          return err
        })
    })
    // todo对于单独某个方法封装的拦截器
  }
  get<T = any>(config: YcRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' })
  }
  post<T = any>(config: YcRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' })
  }
  delete<T = any>(config: YcRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' })
  }
  patch<T = any>(config: YcRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'PATCH' })
  }
}

export default YcRequest
